package com.hnkywl.sts.action.cwgl;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.cwgl.VoucherType;
import com.hnkywl.sts.entity.cwgl.VoucherItemData;
import com.hnkywl.sts.entity.sfgl.Sfk;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.cwgl.CwsfService;
import com.hnkywl.sts.service.cwgl.VoucherService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.UUIDGenerator;
import com.hnkywl.sts.util.XmlParse;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-14
 * Time: 上午9:45
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/cwgl/cwsf")
public class CwsfAction extends SimpleActionSupport {
    private CwsfService cwsfService;
    //收付款实例
    private Sfk cwsf;
    //收付款Id
    private Long cwsfId;
    //收付款类型
    private Integer sfklx = 1;
    //收付款状态
    private String zt = "ysh";
    //单据号码
    private String bm;
    //收付款日期
    private Date sfkrqStart;
    private Date sfkrqEnd;
    //申报公司机构名称
    private String sbgs;
    //申报公司机构Id
    private Long sbgsId;
    //关联单位名称
    private String gldwMc;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows = 10;

    //操作人
    private String czr;
    //操作日期
    private Date cwsfSystemDate;
    //公司名称
    private String gsmc;

    private List<VoucherItemData> voucherItems;

    private Map<String, Object> voucher;
    //附单据数
    private Integer attachment;

    private Integer revokeType;

    private Yg yg;

    private YgService ygService;

    private XmlParse xmlParse;

    private VoucherService voucherService;

    private Map<String, String> result = new HashMap<String, String>();

    private Map<String, Object> voucherMaster = new HashMap<String, Object>();

    private String bz;

    private Gson gson = new Gson();

//    private Long cwsfpjlx;

    private String cwsfpjlxMc;

    private Integer cwsfdrZt;

    private String systemDate;


    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;

    @Action("cwsf")
    public String cwsf(){
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    //收付款的查询分页
    @Action("cwsfList")
    public void cwsfList() {
        this.renderJson(this.cwsfService.findAllCwsf(sfklx, zt, bm, sfkrqStart,sfkrqEnd, sbgs, sbgsId, gldwMc,cwsfpjlxMc, cwsfdrZt,page, rows));
    }

    @Action("cwsf_content")
    public String cwsfContent() {
        yg = ygService.findYgDetail(YgService.getCurrentYg().getId());
        gsmc = this.cwsfService.findCwsfDetail(cwsfId).getSbgs().getJc();
        DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        systemDate =fmt.format(new Date()) ;
        voucherMaster = this.cwsfService.getVoucherMaster(cwsfId, attachment, null, null, null, yg);
        return SUCCESS;
    }

    //查询财务收付分录
    @Action("findcwsffl")
    public void findCwsfFl(){
        renderText(cwsfService.findCwsfFl(cwsfId));
    }



    //财务收付导入
    @Action("generate_voucher")
    public void generateVoucher() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy");
        SimpleDateFormat simpleDateFormat3 = new SimpleDateFormat("MM");
        try {
            if (StringUtils.isEmpty(YgService.getCurrentYgJiuqiUsername())) {
                throw new RuntimeException("您没有U8用户名，不具有导入权限");
            }
            if (StringUtils.isEmpty(YgService.getCurrentYgJiuqiPassword())) {
                throw new RuntimeException("您没有U8密码，不具有导入权限");
            }
            String uuid = UUIDGenerator.getUUID();
            Integer year = GregorianCalendar.getInstance().get(Calendar.YEAR);
            Integer mouth = GregorianCalendar.getInstance().get(Calendar.MONTH);
            yg = ygService.findYgDetail(YgService.getCurrentYg().getId());
            Boolean begin = voucherService.beginGenerateVoucher(cwsfId, VoucherType.SFK);
            if (!begin)
                throw new RuntimeException("其他用户已经开始导入该凭证，请选择其他凭证进行导入!");
            //凭证分录
//            voucher = this.cwsfService.getVoucher(cwsfId, uuid, null);
            voucher = this.cwsfService.getU8VoucherItems(cwsfId, uuid, null,cwsfSystemDate);
            //凭证头
            Map<String, Object> msgheader = voucherService.getMsgHeader();
            voucher.put("msgheader", msgheader);
            //凭证主表信息
            Map<String, Object> voucherMaster =
                    this.cwsfService.getVoucherMaster(cwsfId,null, uuid,null,null,yg);
            //将头部可以修改的部分的修改值重新赋值
            voucherMaster.put("createdate",simpleDateFormat.format(cwsfSystemDate));
            voucherMaster.put("acctyear",simpleDateFormat2.format(cwsfSystemDate));
            voucherMaster.put("acctperiod",simpleDateFormat3.format(cwsfSystemDate));
            voucherMaster.put("attachment",attachment);
            voucherMaster.put("memo", VoucherType.SFK.getTag()+cwsfId);//备注，在凭证删除时使用
            this.cwsfService.cwsfChangeDrrq(cwsfId,cwsfSystemDate);
            voucher.put("master", voucherMaster);
            String r = voucherService.generateVoucher(voucher);
            result = xmlParse.getContent(r);
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
                String vchrid = result.get(XmlParse.VCHRID);
                String vchrnum = result.get(XmlParse.VCHRNUM);
                Boolean success = voucherService.generateVoucherSuccess(uuid, vchrid, vchrnum, year, cwsfId, VoucherType.SFK);
                if (!success)
                    throw new RuntimeException("凭证导入成功，但凭证信息回写失败！");
            }
        } catch (Exception e) {
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        renderJson(gson.toJson(result));
    }


    /**
     * 撤销凭证
     */
    @Action("revoke_voucher")
    public void revokeVoucher() {
        try {
            voucherService.checkJiuqiUsernamePassword();
            Boolean begin = voucherService.beginRevokeVoucher(cwsfId, VoucherType.SFK);
            if (!begin)
                throw new RuntimeException("其他用户已经开始撤销该凭证，请选择其他凭证进行撤销!");
            Map<String, Object> msgheader = voucherService.getMsgHeader();
            Map<String, Object> revokeInfo = this.cwsfService.getRevokeInfo(cwsfId);
            Map<String, Object> info = new HashMap<String, Object>();
            info.put("msgheader", msgheader);
            info.put("revokeinfo", revokeInfo);
            revokeInfo.put("memo", VoucherType.SFK.getTag()+cwsfId);//备注，在凭证删除时使用
            String r = voucherService.revokeVoucher(info);
            result = xmlParse.getContent(r);
            //结果回写
            if ("1".equals(result.get(XmlParse.RESPONSEFLAG))) {
                Boolean success = voucherService.revokeVoucherSuccess(cwsfId, VoucherType.SFK);
                if (!success)
                    throw new RuntimeException("凭证撤销成功，但凭证状态回写失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.put(XmlParse.RESPONSEFLAG, "0");
            result.put(XmlParse.RESPONSEMESSAGE, e.getMessage());
        }
        renderJson(gson.toJson(result));
    }

    /**
     * 重置凭证
     */
    @Action("rest_voucher")
    public void restVoucher() {
        renderJson(voucherService.restVoucher(cwsfId, VoucherType.SFK));
    }

    //=================getter and setter=============

    public Sfk getCwsf() {
        return cwsf;
    }

    public void setCwsf(Sfk cwsf) {
        this.cwsf = cwsf;
    }

    public Long getCwsfId() {
        return cwsfId;
    }

    public void setCwsfId(Long cwsfId) {
        this.cwsfId = cwsfId;
    }

    public Integer getSfklx() {
        return sfklx;
    }

    public void setSfklx(Integer sfklx) {
        this.sfklx = sfklx;
    }

    public String getZt() {
        return zt;
    }

    public void setZt(String zt) {
        this.zt = zt;
    }

    public String getBm() {
        return bm;
    }

    public void setBm(String bm) {
        this.bm = bm;
    }

    public Date getSfkrqStart() {
        return sfkrqStart;
    }

    public void setSfkrqStart(Date sfkrqStart) {
        this.sfkrqStart = sfkrqStart;
    }

    public Date getSfkrqEnd() {
        return sfkrqEnd;
    }

    public void setSfkrqEnd(Date sfkrqEnd) {
        this.sfkrqEnd = sfkrqEnd;
    }

    public String getSbgs() {
        return sbgs;
    }

    public void setSbgs(String sbgs) {
        this.sbgs = sbgs;
    }

    public Long getSbgsId() {
        return sbgsId;
    }

    public void setSbgsId(Long sbgsId) {
        this.sbgsId = sbgsId;
    }

    public String getGldwMc() {
        return gldwMc;
    }

    public void setGldwMc(String gldwMc) {
        this.gldwMc = gldwMc;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }


    public String getCzr() {
        return czr;
    }

    public void setCzr(String czr) {
        this.czr = czr;
    }

    public List<VoucherItemData> getVoucherItems() {
        return voucherItems;
    }

    public void setVoucherItems(List<VoucherItemData> voucherItems) {
        this.voucherItems = voucherItems;
    }

    public Map<String, Object> getVoucher() {
        return voucher;
    }

    public void setVoucher(Map<String, Object> voucher) {
        this.voucher = voucher;
    }

    public Integer getAttachment() {
        return attachment;
    }

    public void setAttachment(Integer attachment) {
        this.attachment = attachment;
    }

    public Date getCwsfSystemDate() {
        return cwsfSystemDate;
    }

    public void setCwsfSystemDate(Date cwsfSystemDate) {
        this.cwsfSystemDate = cwsfSystemDate;
    }

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }

    public Integer getRevokeType() {
        return revokeType;
    }

    public void setRevokeType(Integer revokeType) {
        this.revokeType = revokeType;
    }

    public XmlParse getXmlParse() {
        return xmlParse;
    }

    public void setXmlParse(XmlParse xmlParse) {
        this.xmlParse = xmlParse;
    }

    public VoucherService getVoucherService() {
        return voucherService;
    }

    public void setVoucherService(VoucherService voucherService) {
        this.voucherService = voucherService;
    }

    public Map<String, String> getResult() {
        return result;
    }

    public void setResult(Map<String, String> result) {
        this.result = result;
    }

    public String getGsmc() {
        return gsmc;
    }

    public void setGsmc(String gsmc) {
        this.gsmc = gsmc;
    }

    public Map<String, Object> getVoucherMaster() {
        return voucherMaster;
    }

    public void setVoucherMaster(Map<String, Object> voucherMaster) {
        this.voucherMaster = voucherMaster;
    }

    public String getBz() {
        return bz;
    }

    public void setBz(String bz) {
        this.bz = bz;
    }

    public Gson getGson() {
        return gson;
    }

    public void setGson(Gson gson) {
        this.gson = gson;
    }

//    public Long getCwsfpjlx() {
//        return cwsfpjlx;
//    }
//
//    public void setCwsfpjlx(Long cwsfpjlx) {
//        this.cwsfpjlx = cwsfpjlx;
//    }


    public String getCwsfpjlxMc() {
        return cwsfpjlxMc;
    }

    public void setCwsfpjlxMc(String cwsfpjlxMc) {
        this.cwsfpjlxMc = cwsfpjlxMc;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    public Integer getCwsfdrZt() {
        return cwsfdrZt;
    }

    public void setCwsfdrZt(Integer cwsfdrZt) {
        this.cwsfdrZt = cwsfdrZt;
    }

    public String getSystemDate() {
        return systemDate;
    }

    public void setSystemDate(String systemDate) {
        this.systemDate = systemDate;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setCwsfService(CwsfService cwsfService) {
        this.cwsfService = cwsfService;
    }
    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }
}
